<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
load()->func('tpl');
$shset=pdo_get('zh_gjw_shset',array('uniacid'=>$_W['uniacid']));
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
if (!empty($_GPC['state'])) {
  $where=' WHERE  a.uniacid=:uniacid and a.state=:state  ';
  $data[':uniacid']=$_W['uniacid'];
  $data[':state']=$_GPC['state'];
  $sql="SELECT a.*,b.username,c.dbname FROM ".tablename('zh_gjw_dbassess') . " a"  . " left join " . tablename("zh_gjw_register") . " b on a.u_id=b.id " . " left join ". tablename("zh_gjw_dbpublish") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc";
  $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_dbassess') . " a"  . " left join " . tablename("zh_gjw_register") . " b on a.u_id=b.id " . " left join ". tablename("zh_gjw_dbpublish") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc",$data);
  $active=$_GPC['state'];
}else{
  $where=' WHERE  a.uniacid=:uniacid ';
  $data[':uniacid']=$_W['uniacid'];
  $sql="SELECT a.*,b.username,c.dbname FROM ".tablename('zh_gjw_dbassess') . " a"  . " left join " . tablename("zh_gjw_register") . " b on a.u_id=b.id " . " left join ". tablename("zh_gjw_dbpublish") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc";
  $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zh_gjw_dbassess') . " a"  . " left join " . tablename("zh_gjw_register") . " b on a.u_id=b.id " . " left join ". tablename("zh_gjw_dbpublish") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc",$data);
}
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$dbassesss=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);

if($op=='delete'){
    $res=pdo_delete('zh_gjw_dbassess',array('id'=>$_GPC['id']));
    if($res){
      message('删除成功',$this->createWebUrl('dbassess',array()),'success');
    }else{
      message('删除失败','','error');
    }
}
if($_GPC['op']=='tg'){
  include_once IA_ROOT . '/addons/zh_gjw/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('zh_gjw_dbassess',array('id'=>$_GPC['id'])); 
        /*function getaccess_token($_W){
          $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }*/
        function set_msg($_W,$_GPC){
            //$access_token = getaccess_token($_W);
            $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
            $user=pdo_get('zh_gjw_dbassess',array('id'=>$_GPC['id']));
            $content=mb_substr($user['content'],0,3,'utf-8');
            $openid=pdo_get('zh_gjw_register',array('id'=>$user['u_id']));
            $formwork ='{
                "touser": "'.$openid["openid"].'",
                "template_id": "'.$ress["templeid"].'",
                "form_id":"'.$user['formid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$content.'...",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"抖宝评论审核通过",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ress['accessToken']."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        echo set_msg($_W,$_GPC);
        $res=pdo_update('zh_gjw_dbassess',array('state'=>2),array('id'=>$_GPC['id']));
        if($res){
          message('', 'referer', 'success');
        }else{
              message('失败！','','error');
        }
}
if($_GPC['op']=='jj'){
  include_once IA_ROOT . '/addons/zh_gjw/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_gjw/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('zh_gjw_dbassess',array('id'=>$_GPC['id']));
        
              /*function getaccess_token($_W){
                $res=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                $appid=$res['appid'];
                $secret=$res['appsecret'];
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                $data = curl_exec($ch);
                curl_close($ch);
                $data = json_decode($data,true);
                return $data['access_token'];
              }*/
              function set_msg($_W,$_GPC){
                  //$access_token = getaccess_token($_W);
                  $ress=pdo_get('zh_gjw_system',array('uniacid'=>$_W['uniacid']));
                  $user=pdo_get('zh_gjw_dbassess',array('id'=>$_GPC['id']));
                  $content=mb_substr($user['content'],0,3,'utf-8');
                  $openid=pdo_get('zh_gjw_register',array('id'=>$user['u_id']));
                  $formwork ='{
                      "touser": "'.$openid["openid"].'",
                      "template_id": "'.$ress["templeid"].'",
                      "form_id":"'.$user['formid'].'",
                      "data": {
                          "keyword1": {
                              "value": "'.$content.'...",
                              "color": "#173177"
                          },
                          "keyword2": {
                              "value":"抖宝评论审核未通过",
                              "color": "#173177"
                          }
                      }  
                  }';
                  $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$ress['accessToken']."";
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$url);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
                  curl_setopt($ch, CURLOPT_POST,1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
                  $data = curl_exec($ch);
                  curl_close($ch);
                 return $data;
              }
              echo set_msg($_W,$_GPC);
                $res=pdo_update('zh_gjw_dbassess',array('state'=>3),array('id'=>$_GPC['id']));
                if($res){
                  message('', 'referer', 'success');
                }else{
                      message('失败！','','error');
                }
          
}
    

include $this->template('web/dbassess');